﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeighSystem.Interface
{
    class MotoralaScanHelper
    {
        //https://github.com/Fortelinea/MotorolaCoreScannerNet/issues
        public string GetDescription(this Enum value)
        {
            var fi = value.GetType().GetField(value.ToString());

            var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if ((attributes != null) && (attributes.Length > 0))
                return attributes[0].Description;
            return value.ToString();
        }

    }



    public string GetDescription(this Enum value)
    {
        var fi = value.GetType().GetField(value.ToString());

        var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

        if ((attributes != null) && (attributes.Length > 0))
            return attributes[0].Description;
        return value.ToString();
    }

    private void Instance_DataReceived(object sender, BarcodeScanEventArgs e)
    {
        _lastScanned = e.Data;
        var raw = Encoding.Default.GetString(e.RawData);
        //Console.WriteLine("Barcode type: " + e.BarcodeType.GetDescription());
        Console.WriteLine("Data: " + e.Data);
        FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到扫码数据 :" + e.Data.ToString());
    }

    private void Instance_ImageReceived(object sender, ImageEventArgs e)
    {
        e.Image.Save(ImageSaveLocation, e.Format);
        Console.WriteLine("Image saved to \"{0}\"", ImageSaveLocation);
        FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "收到图片信息");
    }

    private void Instance_ScannerAttached(object sender, PnpEventArgs e)
    {
        FileLog.WriteLogToFile(FileLog.LOGLEVEL.信息, "发现扫码枪接入! id = " + e.ScannerId.ToString());
        _scannerAttached = true;
        ConnectScanners();
    }

    private void Instance_ScannerDetached(object sender, PnpEventArgs e)
    {
        _scannerAttached = false;
    }


    public void BarcodeScannerInit()
    {
        BarcodeScannerManager.Instance.Open();
        BarcodeScannerManager.Instance.RegisterForEvents(EventType.Barcode, EventType.Pnp, EventType.Image, EventType.Other, EventType.Rmd);

        var b = BarcodeScannerManager.Instance.RegisteredEvents;

        //BarcodeScannerManager.Instance.Keyboard.EnableEmulation = false;

        BarcodeScannerManager.Instance.ScannerAttached += Instance_ScannerAttached;
        BarcodeScannerManager.Instance.ScannerDetached += Instance_ScannerDetached;

        var a = BarcodeScannerManager.Instance.DriverVersion;

        ConnectScanners();

        BarcodeScannerManager.Instance.DataReceived += Instance_DataReceived;
        BarcodeScannerManager.Instance.ImageReceived += Instance_ImageReceived;

        //Console.WriteLine("Ready to scan.. (Enter to quit)");
        //Console.ReadLine();
    }


    private void ConnectScanners()
    {
        foreach (var scanner in BarcodeScannerManager.Instance.GetDevices())
        {
            scanner.SetHostMode(HostMode.USB_SNAPI_Imaging);
            if (scanner.Info.UsbHostMode != HostMode.USB_SNAPI_Imaging)
            {
                scanner.SetHostMode(HostMode.USB_SNAPI_Imaging);
                while (_scannerAttached == false)
                {
                    Thread.Sleep(3000);
                }
            }

            //scanner.Defaults.Restore();
            //scanner.CaptureMode = CaptureMode.Barcode;
            //GetAttributes(scanner);

            //PerformCommands(scanner);
            //scanner.Trigger.TriggerByCommand = true;
            //scanner.Trigger.PullTrigger();

            scanner.Actions.SetAttribute(138, 'B', 0); // sound beeper via attribute
        }
    }

    private void PerformCommands(IMotorolaBarcodeScanner scanner)
    {
        scanner.Actions.ToggleLed(LedMode.GreenOn);
        scanner.Actions.ToggleLed(LedMode.GreenOff);
        scanner.Actions.ToggleLed(LedMode.YellowOn);
        scanner.Actions.ToggleLed(LedMode.YellowOff);
        scanner.Actions.ToggleLed(LedMode.RedOn);
        scanner.Actions.ToggleLed(LedMode.RedOff);
        scanner.Actions.SoundBeeper(BeepPattern.FiveHighShort);
        scanner.Reboot();
    }
}




